package com.py.custom.ability.log.util;

import org.slf4j.MDC;

import java.util.UUID;

public class TraceIdUtil {

    public static final String TRACE_ID = "traceId";

    public static final String DEFAULT_TRACE_ID = "";

    public static void setTraceId(String traceId) {
        MDC.put(TRACE_ID, traceId);
    }

    public static String getTraceId() {
        return MDC.get(TRACE_ID);
    }

    public static String genTraceId() {
        String traceId = MDC.get(TRACE_ID);
        if (traceId == null || DEFAULT_TRACE_ID.equals(traceId.trim())) {
            traceId = UUID.randomUUID().toString();
            setTraceId(traceId);
        }
        return traceId;
    }
}
